/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author 013v
 */
public class ReviewManager {

    public boolean AddReview(Review newRev) {
        boolean result = false;

        try {
             int oprod = new ProductManager().getOProdID(newRev.getReviewProductID());
            
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO review(ReviewContent, DateAdded, ReviewerID, ReviewProductID) VALUES(?,NOW(),?,?)");
           
            pstmt.setString(i++, newRev.getReviewContent());
            pstmt.setString(i++, newRev.getReviewerID());
            pstmt.setInt(i++, oprod);

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean delReview(Review newRev) {
        boolean result = false;

        try {

            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM review WHERE ReviewID = " + newRev.getReviewID());

            pstmt.executeUpdate();

            result = true;

            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }
    
    public ResultSet getReviews(String prd){
       
        try{
            int oprod = new ProductManager().getOProdID(prd);
            
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT ReviewContent, DATE_FORMAT(DateAdded, '%b %e ')as 'DateReview', DATE_FORMAT(DateAdded, '%Y %r') as 'TimeReview', Username FROM review r left join user u on r.ReviewerID = u.AccountID WHERE ReviewProductID = ? order by 2 asc, 3 desc");
            pstmt.setInt(1, oprod);
            ResultSet res = pstmt.executeQuery();


            return res;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    
    public boolean checkIfPurchased(String prd, String ud){
        boolean result = false;
        try{
            int oprod = new ProductManager().getOProdID(prd);
            String ouid = new UserManager().getAccountIDFromUser(ud);
            
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            String que = "SELECT p.ProductID as 'ProdID' FROM purchaseditems p, transaction t WHERE t.TransactionID = p.TransactionID AND t.AccountID = ?";
            PreparedStatement pstmt = conn.prepareStatement(que);
            pstmt.setString(1, ouid);
            ResultSet res = pstmt.executeQuery();
            
            while(res.next()){
                if(res.getInt("ProdID") == oprod)
                     result = true;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        return result;
    }
}
